<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>

<script type="text/javascript">
        var wins4ozTreeGrid = $.messager.progress({
            title:'提示',
            msg:'正在加载案件,请等待'
        });
		var treegrid = $("#oz_treegrid");
	    $('#pid2').combotree({
	    	url : '${ctx}/oz/tree.do',
	    	parentField : 'pid',
	    	lines : true,
	    	panelHeight : 'auto',
	    	value : '${oz.pid}',
	    	onLoadSuccess : function() {
	    		parent.$.messager.progress('close');
	    	}
	    });
    
        function collapseAll(){
            $('#oz_treegrid').treegrid('collapseAll');
        }
        function expandAll(){
            $('#oz_treegrid').treegrid('expandAll');
        }
        
        function addOzFun() {
        	var dynamic_add_dialog  = $("<div/>").dialog({
			    title: '部门添加',
			    width: 800,
			    height: 500,
			    closed: false,
			    cache: false,
			    href: '${ctx}/oz/addTo',
			    modal: true,
			    buttons:[{
					text : '添加',
					iconCls: 'icon-add',
					handler : function() {
						$.serializeObject($('#ozadd_Form'));
						$('#ozadd_Form').form('submit',{
							url:'${ctx}/oz/save.do',
							onSubmit:function(){
								return $('#ozadd_Form').form('validate');
							},
							success:function(data){
								var obj = $.parseJSON(data);
								if(obj.success){
									dynamic_add_dialog.dialog('close');
									treegrid.treegrid('reload');
									//treegrid.treegrid('append',{
										//parent: node.id,  // the node has a 'id' value that defined through 'idField' property
										//data: node
									//});
								}
								$.messager.show({
									title:'提示',
									msg:obj.msg
								});
							}
						});
					}
				} ],
				onClose:function(){
					$(this).dialog('destroy');
				}
			});
    	}
        window.oz_pid = '';
        function editOz(){
    		
    		var node = treegrid.treegrid('getSelected');
    		if(node!=null){
    		var ids = "";
    			var dynamic_edit_dialog  = $("<div/>").dialog({
    			    title: '部门编辑',
    			    width: 800,
    			    height: 500,
    			    closed: false,
    			    cache: false,
    			    href: '${ctx}/oz/editTo.do',
    			    modal: true,
    			    buttons:[{
    					text : '编辑',
    					iconCls: 'icon-add',
    					handler : function() {
    						$.serializeObject($('#ozEdit_edit_form'));
    						$('#ozEdit_edit_form').form('submit',{
    							url:'${ctx}/oz/edit',
    							onSubmit:function(){
    								return $('#ozadd_Form').form('validate');
    							},
    							success:function(data){
    								var obj = $.parseJSON(data);
    								if(obj.success){
    									dynamic_edit_dialog.dialog('close');
    									treegrid.treegrid('reload');
    									//treegrid.treegrid('append',{
    										//parent: node.id,  // the node has a 'id' value that defined through 'idField' property
    										//data: node
    									//});
    								}
    								$.messager.show({
    									title:'提示',
    									msg:obj.msg
    								});
    							}
    						});
    					}
    				} ],
    				onClose:function(){
    					$(this).dialog('destroy');
    				},
    				onLoad:function(){
    					$("#ozEdit_edit_form").form('load',node);
                        if(node.pid){
                            window.oz_pid = node.pid;
                        }
    				}
    			    
    			});
    		}else{
    			$.messager.alert('提示','请选择一条要编辑的记录！','info');
    		}
    	}

    	function removeOz() {
    		var treegrid = $("#oz_treegrid");
    		var node = treegrid.treegrid('getSelected');
    		if(node!=null){
    			$.messager.confirm('确认','确认要删除吗？',function(r){
    			    if (r){
    					$.ajax({
    						url:'${ctx}/oz/del',
    						data:{
    							ids:node.id
    						},
    						dataType:'json',
    						success:function(data){
    							treegrid.treegrid('reload');
    							treegrid.treegrid('unselectAll');//使全部的行形成未選中狀態
    							$.messager.show({
    								title:'提示',
    								msg:data.msg
    							});
    						}
    					});
    			    }
    			});
    		}else{
    			$.messager.alert('提示','请选择一行来做删除操作！','info');
    		}
    	}
    </script>


    <table id="oz_treegrid" class="easyui-treegrid"  title="组织机构列表" 
            data-options="
            	border:false,
            	fit:true,
                iconCls: 'icon-ok',
                rownumbers: true,
                collapsible: true,
                fitColumns: true,
                url: '${ctx}/oz/treegrid',
                method: 'post',
                idField: 'id',
                parentField : 'pid',
                treeField: 'text',
                onLoadSuccess:function(){
                    $.messager.progress('close');
                }
                ,
                toolbar : [ {
                text : '添加',
                iconCls : 'icon-add',
                handler : function() {
                addOzFun();
                }
                }, '-', {
                text : '编辑',
                iconCls : 'icon-edit',
                handler : function() {
                editOz();
                }
                }, '-', {
                text : '删除',
                iconCls : 'icon-remove',
                handler : function() {
                removeOz();
                }
                } , '-', {
                text : '折叠',
                iconCls : 'resultset_previous',
                handler : function() {
                collapseAll();
                }
                } , '-', {
                text : '展开',
                iconCls : 'resultset_next',
                handler : function() {
                expandAll();
                }
                }]
            ">
        <thead>
            <tr>
                <th data-options="field:'text',width:30">机构名称</th>
                <th data-options="field:'mainman',width:10">站长/大队长</th>
                <th data-options="field:'address'">地址</th>
                <%--<th data-options="field:'code'">地址</th>--%>
                <th data-options="field:'susong'">诉讼法院</th>
                <th data-options="field:'fuyi'">复议机构</th>
            </tr>
        </thead>
    </table>
